﻿using System;
using System.Xml.Xsl;
using Demo.Services.Contracts.DTO.Filters;
using Demo.Services.View.Controllers;
using Demo.CommonBase.DTO.Infos;
using Demo.CommonBase.Logging;
using Demo.CommonBase.View.Forms;
using Demo.CommonBase.Exceptions;
using System.Reflection;
using Ext.Net;
using Demo.Services.Contracts.DTO.Infos;

namespace Demo.Web.Catalogs
{
    public partial class Resource : PageBase
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {                
            }

        }       
        private ResourceFilter GetFilter()
        {
        
            ResourceFilter filter = new ResourceFilter();
            filter.Name = txtName.Text;
            filter.LastName = txtLastName.Text;
            filter.SecondLastName = txtSecondLastName.Text;
            filter.Cost = txtCost.Text == string.Empty ? 0: Convert.ToDecimal(txtCost.Text);
            filter.PhoneNumber = txtPhoneNumber.Text;
            filter.BirthDate = txtBirthDate.SelectedValue == null ? string.Empty : GetFormatDate(txtBirthDate.SelectedValue.ToString());
            return filter;
        }
        protected void DsResourceReadData(object sender, StoreReadDataEventArgs e)
        {
            ResourceFilter filter;
            ResourceController controller;
            PaginationInfo resourcePaging;
            ResourceResultInfo result;
            try
            {
                Logger.Info();
                filter = GetFilter();
                resourcePaging = new PaginationInfo();

                resourcePaging.PageNum = e.Start;
                resourcePaging.PageSize = e.Limit;
                DataSorter sort = e.Sort[0];
                resourcePaging.Sort = sort.Property;
                resourcePaging.Direction = sort.Direction.ToString(); 
                SetValue("ResourceFilter", filter);
                SetValue("ResourcePagin", resourcePaging);
                controller = new ResourceController();
                controller.ExecuteService(this, "Services.GetResourceList");
                result = (ResourceResultInfo)GetValue("ResourceList");                
                e.Total = result.TotalReg;
                gpResource.GetStore().DataSource = result.ResourceList;

                    
            }catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodBase.GetCurrentMethod(), ex);
            }
        }
        protected void BtnSearchClick(object sender, DirectEventArgs e)             
        {
            dsResource.RemoveAll();
            dsResource.Reload();
        }
        protected void BtnDeleteConfirmClick(object sender, DirectEventArgs e)
        {
            int id = 0;
            if (e.ExtraParams["id"] != null)
            { 
                id = Convert.ToInt32(e.ExtraParams["id"]);
            }
            
            ExtNet.Msg.Confirm("Confirmacion", "Desea Borrar Registro?", new MessageBoxButtonsConfig
            {
                Yes = new MessageBoxButtonConfig
                {
                    Handler = string.Format("Demo.DoYes({0})",id),
                    Text = "Si"
                },
                No = new MessageBoxButtonConfig
                {
                    Handler = "Demo.DoNothing()",
                    Text = "No",                                         
                }
            }).Show();
        }
        [DirectMethod]
        public void DoYes(int id)
        {
            ResourceController controller;
            SetValue("Id", id);
            controller = new ResourceController();
            controller.ExecuteService(this, "Services.DeleteResource");          
           // StatusBarStatusConfig statusConfig = new StatusBarStatusConfig();
           // statusConfig.Icon = Icon.Accept;
           // statusConfig.Text = "El registro fué borrado";
           // statusConfig.Clear2 = true;
           //  FormStatusBar.SetStatus(statusConfig);
            Notification.Show(new NotificationConfig
            {
                Title = "Información",
                Icon = Icon.Information,
                Html = "El registro fué borrado",
                HideDelay = 3000
            });

          
            dsResource.RemoveAll();
            dsResource.Reload();
        }

        [DirectMethod]
        public void DoNothing()
        {           
        }
        [DirectMethod]
        public void SaveAndRefresh(object sender, DirectEventArgs e)             
        {
            Notification.Show(new NotificationConfig
            {
                Title = "Información",
                Icon = Icon.Accept,
                Html = "El registro fué Guardado Exitosamente",
                HideDelay = 3000
            });            
            dsResource.Reload();
        }
        protected void BtnNewClick(object sender, DirectEventArgs e)
        {
            try
            {
                Logger.Info();
                ResourceWindow.Title = "Nuevo Recurso";
                ResourceWindow.Show();
                var loader = new ComponentLoader {AutoLoad = true, Url = "ResourceEdit.aspx"};
                ResourceWindow.LoadContent(loader);

            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodBase.GetCurrentMethod(), ex);
            }

        }
        protected void ToExcel(object sender, EventArgs e)
        {
            string json = GridData.Value.ToString();
            var eSubmit = new StoreSubmitDataEventArgs(json, null);
            var xml = eSubmit.Xml;

            Response.Clear();
            Response.ContentType = "application/vnd.ms-excel";
            Response.AddHeader("Content-Disposition", "attachment; filename=Recurso.xls");
            var xtExcel = new XslCompiledTransform();
            xtExcel.Load(Server.MapPath("..\\ExportConfig\\Excel.xsl"));
            xtExcel.Transform(xml, null, Response.OutputStream);
            Response.End();
        }

    }
}